import * as Router from 'koa-router';
let router = new Router();
import {userModel} from './../model/userModel'

router.get('/', async (ctx: any) => {
    let list = [1,2,3,4,5,6,7,34]
    await ctx.render("index", { title: "base_web", list: list })
    // await next();
})
.get('/admin', async (ctx: any) => {
    let list = [1,2,3,4,5,6,7,34]
    await ctx.render("admin/index")
})
.post('/signin', filter, async (ctx: any) => {
    const {userName, password }:any = ctx.request.body
    console.log(userName, password,'userName, password')
    if(userName == 'admin' && password == '123'){
        await ctx.render("signin-ok")
    }else{
        await ctx.render("signin-failed")
    }
})
.post('/login', async (ctx: any) => {
    let res = ctx.request.body;
    await delay(500);
    console.log(res);
    ctx.body = {code: 100, msg: 'success'}
})
.get('/ok', async (ctx: any) => {
    await delay(1000);
    await ctx.render("signin-ok")
})

async function delay(time){
    return new Promise(resolve => {
        setTimeout(() => {
            resolve()
        }, time);
    })
}

async function filter(ctx: any, next: any){
    const {userName, password }:any = ctx.request.body
    await next();
}
export default router;
